package com.maizhi.controller;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.maizhi.dao.impl.Page;
import com.maizhi.httpmodel.Json;
import com.maizhi.model.Files;
import com.maizhi.service.BaseServiceI;
import com.maizhi.service.FilesServiceI;
import com.maizhi.util.uploadSpringUtil;


@Controller
@RequestMapping("/uploadController")
public class uploadController extends BaseController {
	
	Logger logger = Logger.getLogger(uploadController.class);
	
	BaseServiceI<Files> baseService;
	FilesServiceI fileService;
	
	public BaseServiceI<Files> getBaseService() {
		return baseService;
	}

	@Autowired
	public void setBaseService(BaseServiceI<Files> baseService) {
		this.baseService = baseService;
	}
	
	public FilesServiceI getFileService() {
		return fileService;
	}

	@Autowired
	public void setFileService(FilesServiceI fileService) {
		this.fileService = fileService;
	}

	@RequestMapping(params = "upload")
	public ModelAndView upload(@RequestParam String retrunPath){
		String re = "/"+retrunPath;
		ModelAndView model = new ModelAndView(re);
		//uploadUtil fu = new uploadUtil();
		
		uploadSpringUtil usu = new uploadSpringUtil();
		List<Files> files = usu.upload(request, response);
		logger.info("  files :  "+files);
		if(files!=null && files.size()>0){
			for(Files file : files){
				logger.debug("fileName: "+file.getFileName());
				logger.debug("filePath: "+file.getFilePath());
				logger.debug("fileSuffix: "+file.getFileSubffix());
				baseService.save(file);
			}
		}
		model.addObject("list", files);
		return model;
	}
	
	@RequestMapping(params = "down")
	@ResponseBody
	public Json down(Files file){
		Json json = new Json();
		Files files = baseService.get(Files.class,file.getId());
		if(files!=null){
			uploadSpringUtil usu = new uploadSpringUtil();
			try {
				String dmsg = usu.dowload(files, request, response);
				json.setMsg(dmsg);
				json.setSuccess(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			json.setMsg("数据库找不到相应文件");
		}
		return json;
	}
	
	@RequestMapping(params = "filesList")
	public ModelAndView filesList(Page page){
		 ModelAndView model = new ModelAndView("/files");
		 model.addObject("filesList",fileService.getFilesList(page));
		 return model;
	}
	
	@RequestMapping(params = "filesDel")
	@ResponseBody
	public Json filesDel(@RequestParam String id){
		 Json json = new Json();
		 try{
			 Files files = baseService.get(Files.class,id);
			 fileService.delete(files);
			 json.setSuccess(true);
			 json.setMsg("删除成功");
		 }catch(Exception e){
			 json.setMsg("删除异常 : "+e.getMessage());
		 }
		 return json;
	}
	
	
	
	@RequestMapping(params = "upload2")
	public ModelAndView upload2(){
		ModelAndView model = new ModelAndView("/upload2");	
		String str="过了一下 ";
		model.addObject("msg",str);
		return model;
	}

	
}
